<?php
declare (strict_types = 1);

namespace app\model\concern;

use app\constant\LogicConstant;
use app\facade\ArraysFacade;
use app\facade\StringFacade;

trait LogTrait
{
    /**
     * log_json
     *
     * @param $value
     * @return array
     */
    public function setLogJsonAttr($value)
    {
        if (!empty($value)) {
            if (
                isset($value['relation_data']) && !empty($value['relation_data'])
            ) {
                foreach ($value['relation_data'] as $key => $item) {
                    if (isset($item['data_type'])
                        && LogicConstant::LOG_DATA_TYPE_TWO == $item['data_type']
                        && !empty($item['data'])
                    ) {
                        // 图片
                        $value['relation_data'][$key]['data']['value'] = array_map(function ($item) {
                            return StringFacade::replaceDomainShowUrl($item);
                        }, $value['relation_data'][$key]['data']['value']);
                    }
                }
            }
        }
        return $value;
    }

    /**
     * log_json 获取
     *
     * @param $value
     * @return mixed|string
     */
    public function getLogJsonAttr($value)
    {
        if (!empty($value)) {
            // 记录数据替换
            if (isset($value['relation_data']) && !empty($value['relation_data'])) {
                $value['relation_data'] = array_map(function ($item) {
                    return ArraysFacade::buildLogLogicData($item);
                }, $value['relation_data']);
            }
        }

        // 记录数据
        if (!isset($value['relation_data']))
            $value['relation_data'] = [];

        return $value;
    }
}
